package com.googlecode.economini.converter;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;

public class ArticleConverter extends Converter<ArticleToken> {

  private ArticleConverter(TokenizerNode<ArticleToken> rootNode) {
    super(rootNode);
  }

  public static ArticleConverter newInstance() {
    TokenizerNode<ArticleToken> imgNode = TokenizerNode.build(
        "img",
        AttributeMatcher.any(),
        TokenFactories.attributeTokenFacotry("src", ArticleToken.IMAGE));

    TokenizerNode<ArticleToken> imgDiv = TokenizerNode.buildNodeForPath(
        ImmutableList.of("div"),
        ImmutableList.of(imgNode));

    TokenizerNode<ArticleToken> headingNode = TokenizerNode.build(
        "strong",
        AttributeMatcher.none(),
        TokenFactories.valueTokenFacotry(ArticleToken.HEADING));

    TokenizerNode<ArticleToken> formatNode = TokenizerNode.build(
        "",
        AttributeMatcher.any(),
        TokenFactories.valueTokenFacotry(ArticleToken.FORMATTED_TEXT));

    TokenizerNode<ArticleToken> textNode = TokenizerNode.build(
        "p",
        AttributeMatcher.none(),
        TokenFactories.valueTokenFacotry(ArticleToken.TEXT),
        ImmutableList.of(headingNode, formatNode));

    TokenizerNode<ArticleToken> articleDiv = TokenizerNode.build(
        "div",
        new AttributeMatcher.Builder()
          .withAttribute("class", "ec-article-content clear")
          .build(),
        null,
        ImmutableList.of(textNode, imgDiv));

    TokenizerNode<ArticleToken> rootNode = TokenizerNode.buildNodeForPath(
        ImmutableList.copyOf(Splitter.on('/').split(
            "root/html/body/div/div/div/div/div")),
        ImmutableList.of(articleDiv));

    return new ArticleConverter(rootNode);
  }
}
